########################################################
### Varieties of Economic Vulnerability  				 			 
### Melina Altamirano, Sarah Berens & Franziska Deeg 	 			 
### March 2021         											 
### Conjoint Datamanagement
########################################################

library(readxl)
library(cjoint)
library(dplyr)

###Load BASE_PUEBLA_QUERETARO_FINAL data set and Conjoint_working_file_050320_numeric_subsample_final

Mexico_data <- BASE_PUEBLA_QUERETARO_FINAL

##Vote Choice MEX data
table(Mexico_data$Q10_2) 
Mexico_data$vote_choice <- NA
Mexico_data$vote_choice[which(Mexico_data$Q10_2==0)] <- 1 #1"Didn't vote"
Mexico_data$vote_choice[which(Mexico_data$Q10_2==1)] <- 2 #2"Anaya PAN"
Mexico_data$vote_choice[which(Mexico_data$Q10_2==2)] <- 3 #3"Meade PRI"
Mexico_data$vote_choice[which(Mexico_data$Q10_2==3)] <- 4 #4"AMLO"
Mexico_data$vote_choice[which(Mexico_data$Q10_2==4)] <- 5 #5"Calderon"
Mexico_data$vote_choice[which(Mexico_data$Q10_2==5)] <- 6 #6"Other"
Mexico_data$vote_choice[which(Mexico_data$Q10_2==6)] <- 7 #7"blank ballot"
Mexico_data$vote_choice[which(Mexico_data$Q10_2==7)] <- 8 #8"None"
Mexico_data$vote_choice[which(Mexico_data$Q10_2==99)] <- NA
table(Mexico_data$vote_choice)

Mexico_data$resp_id <- 1:nrow(Mexico_data) 

subset_Mex <- subset(Mexico_data, select = c(vote_choice, resp_id))

Conjoint_no_NA <- left_join(Conjoint_working_file_050320_numeric_subsample_final, subset_Mex, by = "resp_id")


##Labeling Conjoint

Conjoint_no_NA$acceso <- factor(Conjoint_no_NA$acceso,
                                levels = c(1,2,3),
                                labels = c( "Universal", "Contribution based", "Targeted"))
Conjoint_no_NA$financiar <- factor(Conjoint_no_NA$financiar,
                                   levels = c(1,2,3),
                                   labels = c("Contributions","Increase tax","Increase in rich taxing"))
Conjoint_no_NA$hacer <- factor(Conjoint_no_NA$hacer,
                               levels = c(1,2,3),
                               labels = c("Increase benefit","Reduce benefit","Keep benefit same"))
Conjoint_no_NA$politica <- factor(Conjoint_no_NA$politica,
                                  levels = c(1,2,3,4,5),
                                  labels = c("Pensions","Health Care","Housing Credit","Targeted poor households","Child Care"))

Conjoint_no_NA <- rename(Conjoint_no_NA, "Access" = "acceso")
Conjoint_no_NA <- rename(Conjoint_no_NA, "Finance" = "financiar")
Conjoint_no_NA <- rename(Conjoint_no_NA, "Benefits" = "hacer")
Conjoint_no_NA <- rename(Conjoint_no_NA, "Policy" = "politica")


#Change Baseline

Conjoint_no_NA$Access <- relevel(Conjoint_no_NA$Access, "Contribution based")
Conjoint_no_NA$Finance <- relevel(Conjoint_no_NA$Finance,"Contributions")
Conjoint_no_NA$Benefits <- relevel(Conjoint_no_NA$Benefits,"Keep benefit same")
Conjoint_no_NA$Policy <- relevel(Conjoint_no_NA$Policy,"Health Care")
